import axios from 'axios'
let baseUrl = ""
let $pre = ""
// 判断是否是开发环境
if (process.env.NODE_ENV === "development") {
    baseUrl = "/api"
    $pre = "http://localhost:1000"
}
export { $pre }
// 请求拦截
axios.interceptors.request.use(config => {
    // tolen拦截
    if (config.url != '/api/login' && config.url != '/api/register') {
        config.headers.authorization = JSON.parse(sessionStorage.getItem("userInfo")).token
    }
    // 补基础路径
    config.url = baseUrl + config.url
    return config
})
// 响应拦截
axios.interceptors.response.use(res => {
    // 打印
    console.group("request url:" + res.config.url)
    console.log(res);
    console.groupEnd()
    // 处理掉线
    if (res.data.msg == '登录已过期或访问权限受限') {
        // 清空本地的登录信息
        sessionStorage.removeItem("userInfo")
    }

    return res;
})
// 封装get函数
export const get = (url, params = {}) => {
    return axios({ url, params })
}
// 封装post
export const post = (url, params, haveFile = false) => {
    let d = null;
    if (haveFile) {
        d = new FormData()
        for (let key in params) {
            d.append(key, params[key])
        }
    } else {
        d = params
    }
    return axios({
        url,
        method: "post",
        data: d
    })
}